package iii.myangeloldman;

import java.io.File;

import packet.ReportBack;
import packet.ReportBack2;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class EatmedicneActivity extends Activity {
	MediaPlayer mp;
	String path = "";
	String PicPath = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_eatmedicne);
		InitComp();
		SharedPreferences table1 = getSharedPreferences("set0", 0);
		String fName = "";
		fName = table1.getString("picF", "");
		path = Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES).toString()
				+ "/" + table1.getString("soundF", "");
		PicPath = Environment.getExternalStoragePublicDirectory(
				Environment.DIRECTORY_PICTURES).toString()
				+ "/" + table1.getString("picF", "");
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				if (mp == null)
					mp = new MediaPlayer();
				try {
					mp.reset();
					mp.setDataSource(path);
					mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
					mp.prepare();
					mp.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		try {
			File imageDir = Environment
					.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
			Uri uri = Uri.fromFile(new File(imageDir, fName));
			Bitmap bitmap = BitmapFactory.decodeFile(PicPath);
			imgbtn1.setImageBitmap(bitmap);

			String[] path1 = { PicPath };
			callMediaScanner(path1);
//			Toast.makeText(EatmedicneActivity.this, PicPath, Toast.LENGTH_LONG)
//					.show();

		} catch (Exception e) {
			e.printStackTrace();
		}
		t.start();

		// 最後執行
		// for (int i = 0; i < 60*5; i++) {
		// try {
		// Thread.sleep(1000);
		// if(change){
		// break;
		// }
		// } catch (InterruptedException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }
		// }
		// if(!change){
		// SharedPreferences table11 = getSharedPreferences("set0", 0);
		// ReportBack2 r=new ReportBack2();
		// r.oldid=1;
		// r.medid=table11.getInt("Medid", 000);
		// MainActivity.lcl.send(r);
		//
		// Intent go=new Intent(EatmedicneActivity.this,MainActivity.class);
		// startActivity(go);

		// }

	}

	private void callMediaScanner(String[] paths) {
		MediaScannerConnection.scanFile(this, paths, null,
				new MediaScannerConnection.OnScanCompletedListener() {
					public void onScanCompleted(String path, Uri uri) {
						Log.i("ExternalStorageEx", "Scanned " + path + ":");
						Log.i("ExternalStorageEx", "-> uri=" + uri);
					}
				});
	}

	OnClickListener btnNext_click = new OnClickListener() {
		private Handler handler = new Handler();
		private boolean isNext = false;
		{
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(5*60 * 1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (isNext != true) {
						handler.post(new Runnable() {
							@Override
							public void run() {
								XXXX();
							}
						});
					}
				}
			}).start();
		}

		private void XXXX() {
			SharedPreferences table11 = getSharedPreferences("set0", 0);
			ReportBack2 r = new ReportBack2();
			r.oldid = 1;
			r.medid = table11.getInt("Medid", 000);
			MainActivity.lcl.send(r);

			Intent go = new Intent(EatmedicneActivity.this, MainActivity.class);
			startActivity(go);
		}

		@Override
		public void onClick(View v) {
			isNext = true;
			try {
				// Toast.makeText(EatmedicneActivity.this, "吃藥了",
				// Toast.LENGTH_SHORT).show();
				// 這寫用藥回報
				SharedPreferences table1 = getSharedPreferences("set0", 0);
				ReportBack r = new ReportBack();
				r.oldid = 1;
				r.medid = table1.getInt("Medid", 000);
				MainActivity.lcl.send(r);

//				Toast.makeText(EatmedicneActivity.this,
//						r.oldid + "**" + r.medid + " to sql",
//						Toast.LENGTH_SHORT).show();

				Intent it = new Intent(EatmedicneActivity.this,
						MainActivity.class);
				startActivity(it);
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	};

	private void InitComp() {
		imgbtn1 = (ImageButton) findViewById(R.id.btnNext);
		imgbtn1.setOnClickListener(btnNext_click);
	}

	ImageButton imgbtn1 = null;

}
